****** Variable referred, edad de madre
g fn_madre=date( Fechadenacimiento, "DMY")
edit Fechadenacimiento fn_madre
format fn_madre %td
edit Fechadenacimiento fn_madre
g motherage=fn_neo-fn_madre
sum motherage
replace motherage=motherage/365
sum motherage
sum motherage if motherage<50
replace motherage=. if motherage>55
sum motherage
tab referral_success
g referred=(referral_success)
tab referred
replace referred=1 if referred==2
tab referred
replace referred=0 if referred==.
tab referred
duplicates report open_neo
tab inclusion
label var motherage ageofmotheratdelivery


**** Variable Partos
rename Totalnumberofdeliveredbirths partos
rename Q partos_obsdatetime
rename R partos_1
rename S partos_1_obsdatetime

g fecha_parto=date(partos_obsdatetime, "DMY")
g fecha_parto_1=date(partos_1_obsdatetime, "DMY")
format fecha_parto %td
format fecha_parto_1 %td

** Algunas madres tienen 2 observaciones de numero de parto
** por lo que hago limpieza de datos para que cada parto 
** corresponda a cada neonato

edit id_madre fn_neo partos fecha_parto partos_1 fecha_parto_1 if partos_1!=.
replace partos=. if fn_neo<fecha_parto

g numberdeliv=.
replace numberdeliv=partos if partos!=.
replace numberdeliv=partos_1 if partos==.
replace numberdeliv=. if duplicados_madre==1
sum numberdeliv
sum numberdeliv if numberdeliv==0
sum numberdeliv if numberdeliv!=0

** Creando variable nulliparous
g nulliparous=(numberdeliv==0)
tab nulliparous

***** Genero neonatos
g gender_f=(gender=="F")

*** Peso neonatal reportado por madre
g birth_w=real(Recalledbirthweight)
label var birth_w recalledbirthweightbymother
replace birth_w=. if birth_w<2
sum birth_w

**** mortalidad neonatal 
tab neo_death

**** neonatal age at referral 
g neo_age_ref=.
label var neo_age_ref neonatalageatreferral
edit open_neo fn_neo NeonatalCallTaxonomy_obsDateti trastornoneonatal_obsDatetime trastornoneonatal_encounterType trastornoneonatal_obsDatetime_ trastorno_neo_encounter_1 trastorno_neo_date_2 trastorno_neo_encounter_2 trastorno_neo_date_3 trastorno_neo_encounter_3 trastorno_neo_date_4 trastorno_neo_encounter_4 if NeonatalCallTaxonomy_obsDateti!=. & referred==1

replace neo_age_ref=(NeonatalCallTaxonomy_obsDateti-fn_neo)

****** Referral Indications
tab neo_indication if referred==1

*********************************************
*********************************************

g consent=(trastornoneonatal_encounterType=="Neonato") 
replace consent=1 if trastorno_neo_encounter_1=="Neonato"
replace consent=1 if trastorno_neo_encounter_2=="Neonato"
replace consent=1 if trastorno_neo_encounter_3=="Neonato"
replace consent=1 if trastorno_neo_encounter_4=="Neonato"


g form_neo=.
label var form_neo sitieneformneonatosegun_var_pesokg
replace form_neo=1 if pesokg_encounterType=="Neonato"

g born_i=.
label var born_i sinacioentresept17ysept18
replace born_i=(fn_neo>td(31/8/2017) & fn_neo<td(1/10/2018))

g included=.
label var included cumplecriterios_born_i_consent
replace included=(born_i==1 & consent==1)


*********
******** TABLA DEMOGRAFICA ***********

** Mother Age at delivery
sum motherage if included==1 & referred==1,detail
sum motherage if included==1 & referred==0,detail
ranksum motherage if included==1, by(referred)

** Nulliparous 
tab nulliparous referred if included==1, col
prtest nulliparous if included==1, by(referred)

** Number deliveries 
ranksum numberdeliv if included==1, by(referred)

** Gender 
tab gender referred if included==1, col chi

** Diferencia de pesos
sum birth_w if referred==1 & included==1, detail
sum birth_w if referred==0 & included==1, detail
ttest birth_w if included==1, by(referred)


** Mortalidad Neonatal 
tab neo_death referred if included==1, col chi 

** Edad neonato al momento de referencia
sum neo_age_ref if included==1, detail


******** Complicaciones Maternas *********** 

g currentcomp_date=date(EU, "DMY")
format currentcomp_date %td

g currentcomp_date_1=date(EX, "DMY")
format currentcomp_date_1 %td

g currentcomp_date_2=date(FA, "DMY")
format currentcomp_date_2 %td

rename FD currentcomp_date_3

****** Fechas de complicaciones deben ser antes de la fecha de nacimiento neonato
*** Algunas pacientes han tenido 2 embarazos por lo que se filtra por fn_neo

g dif_neo_currentcomp=.
label var dif_neo_currentcomp filtrofechanacimientoneonatoconcomplicacionmaterna

replace dif_neo_currentcomp=1 if currentcomp_date<fn_neo

g dif_neo_currentcomp_1=.
label var dif_neo_currentcomp_1 filtrofechanacimientoneonatoconcomplicacionmaterna

replace dif_neo_currentcomp_1=1 if currentcomp_date_1<fn_neo

g dif_neo_currentcomp_2=.
label var dif_neo_currentcomp_2 filtrofechanacimientoneonatoconcomplicacionmaterna

replace dif_neo_currentcomp_2=1 if currentcomp_date_2<fn_neo

g dif_neo_currentcomp_3=.
label var dif_neo_currentcomp_3 filtrofechanacimientoneonatoconcomplicacionmaterna

replace dif_neo_currentcomp_3=1 if currentcomp_date_3<fn_neo


***** Ahora contabilizo los datos de pacientes con consentimiento y complicaciones limpias

** Limpidando complicaciones en el embarazo. Missing == "ninguna"
g compduringpreg=.
replace compduringpreg=1 if currentcomp=="Threatened abortion"
replace compduringpreg=1 if currentcomp=="hemorragia prenatal"
replace compduringpreg=1 if currentcomp=="infecciÃ³n urinaria"
replace compduringpreg=0 if currentcomp=="ninguna"
replace compduringpreg=1 if currentcomp=="presiÃ³n alta"
replace compduringpreg=1 if currentcomp=="ruptura prematura de membranas"
replace compduringpreg=1 if currentcomp=="infecciÃ³n vaginal"


g compduringpreg_1=.
replace compduringpreg_1=1 if currentcomp_1=="Threatened abortion"
replace compduringpreg_1=1 if currentcomp_1=="hemorragia prenatal"
replace compduringpreg_1=1 if currentcomp_1=="infecciÃ³n urinaria"
replace compduringpreg_1=0 if currentcomp_1=="ninguna"
replace compduringpreg_1=1 if currentcomp_1=="presiÃ³n alta"
replace compduringpreg_1=1 if currentcomp_1=="infecciÃ³n vaginal"


g compduringpreg_2=.
replace compduringpreg_2=1 if currentcomp_2=="hemorragia prenatal"
replace compduringpreg_2=1 if currentcomp_2=="infecciÃ³n vaginal"

g compduringpreg_3=.
replace compduringpreg_3=1 if currentcomp_3=="infecciÃ³n urinaria"


***** Variables complicaciones previas durante embarazo incluidas por fecha y por consentimiento

g complication_included=(included==1 & dif_neo_currentcomp==1 & compduringpreg==1)
g complication_included_1=(included==1 & dif_neo_currentcomp_1==1 & compduringpreg_1==1)
g complication_included_2=(included==1 & dif_neo_currentcomp_2==1 & compduringpreg_2==1)
g complication_included_3=(included==1 & dif_neo_currentcomp_3==1 & compduringpreg_3==1)


**** Variable para Analizar:  complicacionesduranteembarazoprevio prev_preg_c

g prev_preg=.
label var prev_preg tuvocomplicacionesduranteembarazoprevio
replace prev_preg=1 if complication_included==1 | complication_included_1==1 | complication_included_2==1 | complication_included_3==1 
replace prev_preg=. if compduringpreg==.
tab prev_preg referred if included==1, col chi

tab prev_preg referred if included==1, col chi


***** Tabla 2. Complicaciones Neonatales y Maternas 
g prenatal_hemorrage=(currentcomp=="hemorragia prenatal" | currentcomp_1=="hemorragia prenatal" | currentcomp_2=="hemorragia prenatal" | currentcomp_3=="hemorragia prenatal")
g threatened_abortion=(currentcomp=="Threatened abortion" | currentcomp_1=="Threatened abortion" | currentcomp_2=="Threatened abortion" | currentcomp_3=="Threatened abortion")
g uti=(currentcomp=="infecciÃ³n urinaria" | currentcomp_1=="infecciÃ³n urinaria" | currentcomp_2=="infecciÃ³n urinaria" | currentcomp_3=="infecciÃ³n urinaria")
g hypertensivedisorder=(currentcomp=="presiÃ³n alta" | currentcomp_1=="presiÃ³n alta" | currentcomp_2=="presiÃ³n alta" | currentcomp_3=="presiÃ³n alta")
g vaginalinfection=(currentcomp=="infecciÃ³n vaginal" | currentcomp_1=="infecciÃ³n vaginal" | currentcomp_2=="infecciÃ³n vaginal" | currentcomp_3=="infecciÃ³n vaginal")
g membranerupture=(currentcomp=="ruptura prematura de membranas" | currentcomp_1=="ruptura prematura de membranas" | currentcomp_2=="ruptura prematura de membranas" | currentcomp_3=="ruptura prematura de membranas")
g ninguna=(currentcomp=="ninguna" | currentcomp_1=="ninguna" | currentcomp_2=="ninguna" | currentcomp_3=="ninguna")

replace prenatal_hemorrage=. if prev_preg==.
replace threatened_abortion=. if prev_preg==.
replace uti=. if prev_preg==.
replace hypertensivedisorder=. if prev_preg==.
replace vaginalinfection=. if prev_preg==.
replace membranerupture=. if prev_preg==.
replace ninguna=. if prev_preg==. 

replace prenatal_hemorrage=0 if prev_preg==0
replace threatened_abortion=0 if prev_preg==0
replace uti=0 if prev_preg==0 
replace hypertensivedisorder=0 if prev_preg==0
replace vaginalinfection=0 if prev_preg==0
replace membranerupture=0 if prev_preg==0



tab prenatal_hemorrage referred if included==1 & duplicado==0, col chi


******* Chi2 para todas las complicaciones maternas juntas
mrtab prenatal_hemorrage threatened_abortion uti hypertensivedisorder vaginalinfection membranerupture ninguna if included==1 & duplicado==0, by(referred) chi


